$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'psnetpoint/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true},
			{ label: '网点名称', name: 'pointName', index: 'point_name', width: 120 },
			{ label: '所属分公司id', name: 'branchCompanyId', index: 'branch_company_id', width: 80 ,hidden:true},
            { label: '负责人', name: 'person', index: 'person', width: 80 },
            { label: '电话', name: 'telphone', index: 'telphone', width: 80 },
            { label: '所属分公司名称', name: 'branchCompany', index: 'branch_company', width: 150 },
			{ label: '网点地址', name: 'address', index: 'address', width: 280 },
			{ label: '营业时间', name: 'shopHours', index: 'shop_hours', width: 80 }, 			
			{ label: '收件时间', name: 'receivingTime', index: 'receiving_time', width: 80 ,hidden:true},
			{ label: '经度', name: 'longitude', index: 'longitude', width: 80 ,hidden:true},
			{ label: '纬度', name: 'latitude', index: 'latitude', width: 80 ,hidden:true},
			{ label: '省', name: 'province', index: 'province', width: 80,hidden:true },
			{ label: '市', name: 'city', index: 'city', width: 80 ,hidden:true},
			{ label: '区', name: 'country', index: 'country', width: 80 ,hidden:true},
			{ label: '高德云图id(高德检索用)', name: 'gaoId', index: 'gao_id', width: 80 ,hidden:true}

        ],
		viewrecords: true,
        height: 700,
        rowNum: 20,
		rowList : [20,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	// $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

    vm.getBranchCompany();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            pointName: null,
            branchCompany:null
        },
		showList: true,
		title: null,
		psNetPoint: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.psNetPoint = {
                branchCompany: null
			};

		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.psNetPoint.id == null ? "psnetpoint/save" : "psnetpoint/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.psNetPoint),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "psnetpoint/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "psnetpoint/info/"+id, function(r){
                vm.psNetPoint = r.psNetPoint;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'pointName': vm.q.pointName,'branchCompany':vm.q.branchCompany},
                page:page
            }).trigger("reloadGrid");
		},


        BranchCompanySelect:function(){
            utils.openDialogView('分公司列表',$("#windowBranchCompany"),'900px','500px',1);
        },
        getBranchCompany: function(){
            $("#jqGridBranchCompany").jqGrid({
                url: baseURL + 'psbranchcompany/list',
                datatype: "json",
                colModel: [
                    { label: 'id', name: 'id', index: 'id', width: 50, key: true},
                    { label: '分公司名称', name: 'companyName', index: 'company_name', width: 100 },
                    { label: '分公司地址', name: 'address', index: 'address', width: 150 },
                    { label: '联系电话', name: 'contactNumber', index: 'contact_number', width: 80 },
                    { label: '联系人', name: 'contactPeople', index: 'contact_people', width: 80 }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 10,
                rowList : [10,30,50],
                rownumbers: true,
                // rownumWidth: 85,
                autowidth:false,
                multiselect: false,
                pager: "#jqGridPagerBranchCompany",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    // $("#jqGridBranchCompany").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });
        },

	}
});

/** 分公司弹窗确定和关闭按钮处理 **/
$("#addBranchCompany").click(function(){
    //判断是否选中了数据
    var id=$("#jqGridBranchCompany").jqGrid('getGridParam','selrow');
    if(id==null){
        alert("请选择一条记录!");
        return ;
    }
    var rowData = $("#jqGridBranchCompany").jqGrid('getRowData',id);
    vm.psNetPoint.branchCompany = rowData.companyName;
    vm.psNetPoint.branchCompanyId = rowData.id;
    utils.closeCurrentPage();
});

$("#closeBranchCompany").click(function(){
    utils.closeCurrentPage();
});